package test.v2;

import com.baishui.beans.factory.support.DefaultBeanFactory;
import org.junit.Test;

import java.beans.*;

public class MyMain {

    /**
     * 测试 Introspector 方法
     * @see DefaultBeanFactory#populateBean(com.baishui.beans.BeanDefinition, java.lang.Object)
     */
    @Test
    public void test00() throws Exception{
        Dog dog = new Dog();
        dog.setDogColor("white");
        BeanInfo beanInfo = Introspector.getBeanInfo(Dog.class);
        BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
        EventSetDescriptor[] eventSetDescriptors = beanInfo.getEventSetDescriptors();

        Object dogColor = beanDescriptor.getValue("dogColor");

        for (PropertyDescriptor p: propertyDescriptors) {
            System.out.println(p.getPropertyType().getName());
//            System.out.println(p.getName());
            if("name".equalsIgnoreCase(p.getName())){
                p.getWriteMethod().invoke(dog,"hahah");
            }
        }
        System.out.println(dog);

        for (MethodDescriptor m: methodDescriptors) {
            System.out.println(m.getName());
        }


    }
}

class Animal{

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Dog extends Animal{
    private String dogColor;

    public String getDogColor() {
        return dogColor;
    }

    public void setDogColor(String dogColor) {
        this.dogColor = dogColor;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "dogColor='" + dogColor + '\'' +
                '}';
    }
}
